import {
	getConfig
} from "@/api/public.js"
import {
	getUserInfo,
	getUserCenterDetail
} from "@/api/user.js"
const TOKEN_KEY = "user_token";
const lng_key = "wm_lng_key"
const lat_key = "wm_lat_key"
const BACK_KEY = "back_url"
const back_login_key = "back_login_key"
const spreadUid_KEY = "spread_uid_key"
const uid_key = "wm_uid_key"
const bank_code_key = "wm_bank_code"
const bank_name_key = "wm_bank_name"
const bank_user_name_key = "wm_bank_user_name"
const action_key = "action_key"
const state = {
	uid: uni.getStorageSync(uid_key) || 0,
	token: uni.getStorageSync(TOKEN_KEY) || '',
	url: "",
	longitude: uni.getStorageSync(lng_key) || "",
	latitude: uni.getStorageSync(lat_key) || "",
	userInfo: {},
	centerInfo: {},
	backUrl: uni.getStorageSync(BACK_KEY) || "pages/index/index",
	spreadUid: uni.getStorageSync(spreadUid_KEY) || 0,
	currentLogin: false,
	bankCode: uni.getStorageSync(bank_code_key) || '',
	bankName: uni.getStorageSync(bank_name_key) || '',
	bankUserName: uni.getStorageSync(bank_user_name_key) || '',
	action: uni.getStorageSync(action_key) || 'none',
	productId: 0
};

const mutations = {

	LOGIN(state, datas) {
		state.uid = datas.uid
		state.token = datas.token;
		uni.setStorageSync(uid_key, datas.uid)
		uni.setStorageSync(TOKEN_KEY, datas.token)
	},
	LOGOUT(state) {
		state.uid = 0
		state.token = ''
		uni.removeStorageSync(TOKEN_KEY)
		uni.removeStorageSync(uid_key)
	},
	GETCONFIG(state) {

	},
	SETLOCALTION(state, location) {
		state.longitude = location.longitude
		state.latitude = location.latitude
		uni.setStorageSync(lng_key, state.longitude)
		uni.setStorageSync(lat_key, state.latitude)
	},
	CLEARLOCALTION(state) {
		state.longitude = ''
		state.latitude = ''
	},
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
	},
	UPDATE_CENTERINFO(state, centerInfo) {
		state.centerInfo = centerInfo;
	},
	SETBACKURL(state, page) {
		state.backUrl = page || "/pages/index/index"
		uni.setStorageSync(BACK_KEY, state.backUrl)
	},
	SETSPREADUID(state, uid) {
		state.spreadUid = uid
		uni.setStorageSync(spreadUid_KEY, uid)
	},
	SETPRODUCTID(state, id) {
		state.productId = id
	},
	SETACTION(state, action) {
		state.action = action
		uni.setStorageSync(action_key, action)
	},
	SETBACKLOGIN(state, res) {
		state.currentLogin = res
	},
	SETBANKCODE(statue, code) {
		state.bankCode = code
		uni.setStorageSync(bank_code_key, code)
	},
	SETBANKNAME(state, name) {
		state.bankName = name
		uni.setStorageSync(bank_name_key, name)
	},
	SETBANKUSERNAME(state, userName) {
		state.bankUserName = userName
		uni.setStorageSync(bank_user_name_key, userName)
	}

};

const actions = {
	SETBACKURL({
		state,
		commit
	}, page) {
		console.log("page>>>>", page)
		state.backUrl = page || "/pages/index/index"
		uni.setStorage({
			key: BACK_KEY,
			data: page,
			success: function() {
				Promise.resolve(page)
			},
			fail(err) {
				Promise.reject(err)
			}
		})

	},
	USERINFO({
		state,
		commit
	}, force) {
		if (Object.keys(state.userInfo).length > 0 && !force)
			return Promise.resolve(state.userInfo);
		else
			return new Promise(reslove => {
				getUserInfo().then(res => {
					commit("UPDATE_USERINFO", res.data);
					reslove(res.data);
				});
			}).catch(() => {
				dialog.error("获取信息失败!");
			});
	},
	CENTERINFO({
		state,
		commit
	}, force) {
		if (Object.keys(state.centerInfo).length > 0 && !force)
			return Promise.resolve(state.centerInfo);
		else
			return new Promise(reslove => {
				getUserCenterDetail().then(res => {
					commit("UPDATE_CENTERINFO", res.data);
					reslove(res.data);
				});
			}).catch(() => {
				dialog.error("获取信息失败!");
			});
	},
	GETLOCALTION({
		state,
		commit
	}) {
		let lnglat = {
			lng: state.longitude,
			lat: state.latitude
		}
		if (state.latitude != '' && state.longitude != '') {
			lnglat.lng = state.longitude
			lnglat.lat = state.latitude
		}
		return new Promise(reslove => {
			reslove(lnglat)
		})
	}
};



export default {
	state,
	mutations,
	actions
};
